package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.config.ConfigAlt;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class QuizSetTable {
  private static final Log _log = LogFactory.getLog(QuizSetTable.class);
  
  public static int GS_quizId;
  
  public static String GS_showQuiz;
  
  public static String[] GS_option = new String[4];
  
  public static byte GS_answer;
  
  private static QuizSetTable _instance;
  
  public static QuizSetTable getInstance() {
    if (_instance == null)
      _instance = new QuizSetTable(); 
    return _instance;
  }
  
  public final void load() {
    updateQuizInfo();
    _log.info("载入每日一题资料 (quiz_id: " + GS_quizId + ")");
  }
  
  public final void updateQuizInfo() {
    Connection conn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      conn = DatabaseFactory.get().getConnection();
      pstm = conn.prepareStatement("SELECT * FROM william_npc_quiz_day WHERE is_active=1");
      rs = pstm.executeQuery();
      if (rs.next()) {
        GS_quizId = rs.getInt("quiz_id");
        GS_showQuiz = rs.getString("show_quiz");
        GS_option[0] = rs.getString("optionA");
        GS_option[1] = rs.getString("optionB");
        GS_option[2] = rs.getString("optionC");
        GS_option[3] = rs.getString("optionD");
        GS_answer = rs.getByte("answer");
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(conn);
    } 
  }
  
  public final void updateQuizToNext() {
    int quiz_id = 0;
    switch (ConfigAlt.QUIZ_SET_TYPE) {
      case 0:
        return;
      case 1:
        if (checkQuizExist(GS_quizId + 1))
          quiz_id = GS_quizId + 1; 
        break;
      case 2:
        quiz_id = (new Random()).nextInt(getQuizSize());
        break;
    } 
    GS_showQuiz = null;
    GS_option[0] = null;
    GS_option[1] = null;
    GS_option[2] = null;
    GS_option[3] = null;
    GS_answer = 0;
    Connection conn = null;
    PreparedStatement pstm = null;
    PreparedStatement pstm2 = null;
    try {
      conn = DatabaseFactory.get().getConnection();
      pstm = conn.prepareStatement("UPDATE william_npc_quiz_day SET is_active=0");
      pstm.execute();
      pstm2 = conn.prepareStatement("UPDATE william_npc_quiz_day SET is_active=1 WHERE quiz_id=?");
      pstm2.setInt(1, quiz_id);
      pstm2.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm2);
      SQLUtil.close(pstm);
      SQLUtil.close(conn);
    } 
  }
  
  private final boolean checkQuizExist(int quiz_id) {
    Connection conn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      conn = DatabaseFactory.get().getConnection();
      pstm = conn.prepareStatement("SELECT * FROM william_npc_quiz_day WHERE quiz_id=?");
      pstm.setInt(1, quiz_id);
      rs = pstm.executeQuery();
      if (rs.next())
        return true; 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(conn);
    } 
    return false;
  }
  
  private final int getQuizSize() {
    Connection conn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      conn = DatabaseFactory.get().getConnection();
      pstm = conn.prepareStatement("SELECT count(*) FROM william_npc_quiz_day");
      rs = pstm.executeQuery();
      if (rs.next())
        return rs.getInt(1); 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(conn);
    } 
    return 0;
  }
  
  public final void updateAllPcQuizSet() {
    Connection conn = null;
    PreparedStatement pstm = null;
    try {
      conn = DatabaseFactory.get().getConnection();
      pstm = conn
        .prepareStatement("UPDATE character_quests SET quest_step=0 WHERE quest_id=81245 AND quest_step=1");
      pstm.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(conn);
    } 
  }
}
